import {observable,action} from "mobx";
import { ListViewPage} from 'weaver-mobile-page';
import * as Apis from "../../apis/docCenterApi";

class DocCenterStore{
  @observable libraryList = new ListViewPage.Store({ route: '/docLabrary' });  
  @observable todoList = new ListViewPage.Store({ route: '/docLabrary' });  
  @observable tabsData = [];
  @observable hotThemeList = [];
  @observable officalType = "1";//待办的type
  @observable tabs = [] ;
  @observable userId = "";//用户id
  @observable libraryLoading = false;
  @observable todoLoading = false;

  //公文库list数据
  @action.bound 
  getlibraryList(){
    this.libraryLoading = true;
    Apis.odocLibrary().then(data=>{
      if(data.sessionkey){
        this.libraryList.getData({ dataKey:data.sessionkey,pageSize:5},()=>{
          this.libraryLoading = false;
        },true);
      }
    })
  }

  //代办公文list数据
  @action.bound 
  getTodoList(){
    this.todoLoading = true;
    const params = {
      officalType:this.officalType,
    }
    Apis.getTodoList(params).then(data=>{
      if(data.sessionkey){
        this.todoList.getData({ dataKey:data.sessionkey,pageSize:5},()=>{
          this.todoLoading = false;
        },true);
      }
    })
  }

  //获取顶部按钮
  @action.bound 
  getCenterTab(){
    Apis.getCenterTab().then(data=>{
      if(data.api_status){
        this.tabsData = data.mobileTab;
      }
    })
  }

  //获取热点主题
  @action.bound 
  getHotTheme(){
    Apis.getHotTheme().then(data=>{
      if(data.countByTopicType){
        this.hotThemeList = data.countByTopicType.dataList.sort((a, b) => b.count - a.count);
      }
    })
  }

  //获取代办tabs
  @action.bound 
  getTodoTabs(){
    const params = {
      isDocCenter:true
    }
    Apis.getTodoTabs(params).then(data=>{
      if(data.datalist){
        this.tabs = this.changeTabData(data.datalist);
      }
    })
  }

  changeTabData(tabs){
    tabs.map(item=>{
      item.title = `${item.title}(${item.count})`;
    })
    return tabs;
  }

  @action.bound
  changeTab(tab){
    this.officalType = tab.officalType;
    this.getTodoList();
  }

  //获取用户id
  @action.bound
  getUserId(){
    Apis.getUserId().then(data=>{
      if(data.api_status){
        this.userId = data.userId;
      }
    })
  }

}

export default DocCenterStore;
